<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.hxci.web.bean.Conn" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="javax.faces.context.ExternalContext" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ include file="public.jsp"%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String Borrowingid = request.getParameter("Borrowingid");//ID
    String status = request.getParameter("status");//状态
    System.out.println(Borrowingid+" "+status);
    List<String> params = new ArrayList<>();
    List<String> paramsl = new ArrayList<>();
    String sql = "UPDATE borrowings SET status = ? ";
//    更新借阅记录表（borrowings）的状态

    String sql2 = "UPDATE books SET status = ? Where bookid = (SELECT bookid FROM borrowings WHERE Borrowingid = ?)";
//    通过子查询获取图书ID后更新图书表（books）的状态
    params.add(status);// 第一个SQL只需状态参数
    paramsl.add(status); // 第二个SQL需要两个参数（当前只添加了状态）
    if(status.equals("续借")){
        System.out.println("续借");
        String sql3="select returntime from borrowings where Borrowingid = ?";
        List<Object> params3 = new ArrayList<>();
        params3.add(Borrowingid);
        String realPath="C:/webk/code/library-management-system/src/main/webapp/WEB-INF/db.properties";
        Conn c=new Conn(realPath);

        List<Map> list = c.doQuery(sql3,params3);
//        执行查询
        String returntime=list.get(0).get("returntime").toString();
//        提取归还时间,从查询结果的第一条记录中获取 returntime 字段的值
        LocalDate returntime1 = LocalDate.parse(returntime);
        LocalDate newDate = returntime1.plusDays(7);
        //归还时间+一周
        String newDateString = newDate.format(DateTimeFormatter.ISO_DATE);
//            格式转化DateTimeFormatter.ISO_DATE：Java 内置的日期格式化器
        sql+= ",returntime = ? ";
        params.add(newDateString);
//        将新日期字符串添加到参数列表
    }

        sql += " WHERE Borrowingid = ? ";
//    根据借阅ID筛选记录。
//    System.out.println(sql);

        params.add(Borrowingid);
        paramsl.add(Borrowingid);
        dosave(sql2,paramsl);
        dosave(sql,params);

    if(status.equals("已归还")){
    String sql3="delete from borrowings where Borrowingid = ?";
    List<String> params3 = new ArrayList<>();
    params3.add(Borrowingid);
    dosave(sql3,params3);
        System.out.println(sql3+":"+params3);
    }
        if(request.getSession().getAttribute("login_account").equals("admin")){
            response.sendRedirect("rent.jsp");
        }else{
            response.sendRedirect("profile.jsp");
        }

%>
</body>
</html>
